package com.buba.customer.controller;

import com.buba.customer.pojo.China;
import com.buba.customer.pojo.CustomerAddr;
import com.buba.customer.pojo.CustomerLogin;
import com.buba.customer.service.ChinaService;
import com.buba.customer.service.CustomerAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址控制层
 */
@Controller
@RequestMapping("/CustomerAddr")
public class CustomerAddrController {
    private CustomerAddrService customerAddrService;
    private ChinaService chinaService;


    /**
     * 添加地址信息
     * @return
     */
    @RequestMapping("/addCustomerAddr.do")
    @ResponseBody
    public Map<String,Object> addCustomerAddr(HttpSession session,CustomerAddr customerAddr){
        Map<String,Object> map = new HashMap<>();
        //从缓存中获取当前登录的用户id
        CustomerLogin customerLogin = (CustomerLogin) session.getAttribute("customerLogin");
        customerAddr.setCustomerLogin(customerLogin);
        //判断要添加的地址是否设为默认的地址
        if(customerAddr.getIs_default()==1)
            //如果是的话，就把其他的默认地址改为否
            customerAddrService.updIsDefault();
        //把要添加的地址对象传入到业务层
        int count = customerAddrService.addCustomerAddr(customerAddr);
        map.put("msg", "添加了" + count + "数据");
        return map;
    }

    /**
     * 查询地址信息
     * @return
     */
    @RequestMapping("/selCustomerAddr.do")
    @ResponseBody
    public Map<String,Object> selCustomerAddr(HttpSession session,Integer page,Integer limit){
        Map<String,Object> map = new HashMap<>();

        //获取当前正在登录的账号的id
        CustomerLogin customerLogin = (CustomerLogin) session.getAttribute("customerLogin");
        //查询当前正在登陆的用户地址信息
        List<CustomerAddr> list =  customerAddrService.selCustomerAddrByLoginId(customerLogin.getCustomerId());
        //根据地址id查询地址的详细信息。
        for(CustomerAddr customerAddr : list){
            Map<String,China> addrMap = new HashMap<>();
            China province = chinaService.selChinaById(customerAddr.getProvince());
            China city = chinaService.selChinaById(customerAddr.getCity());
            China district = chinaService.selChinaById(customerAddr.getDistrict());
            //把用户的详细地址查询出来后，装入customerAddr对象中
            //数组 集合 尝试使用Map装入地址
            addrMap.put("province",province);
            addrMap.put("city",city);
            addrMap.put("district",district);
            customerAddr.setAddrMap(addrMap);
        }

        //查询总条数，查询当前页的数据
        Long count = customerAddrService.selCount(map);
        //把分页参数携带过去
        map.put("page",(page-1) * limit);
        map.put("limit",limit);

        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    /**
     * 删除地址信息
     * @param ids
     * @return
     */
    @RequestMapping("/delCustomerAddr.do")
    @ResponseBody
    public Map<String,Object> delCustomerAddr(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        Integer count = customerAddrService.delCustomerAddr(split);
        map.put("msg","删除了"+count+"条数据");
        return map;
    }

    /**
     * 修改默认地址
     * @return
     */
    @RequestMapping("updCustomerAddr.do")
    @ResponseBody
    public Map<String,Object> updCustomerAddr(Integer id){
        Map<String,Object> map = new HashMap<>();
        //1、把以前是默认地址的数据改为不默认
        customerAddrService.updIsDefault();
        //2、把要修改的id改为默认
        Integer count = customerAddrService.updCustomerAddr(id);
        map.put("msg","设置默认成功");

        return map;
    }



    public CustomerAddrService getCustomerAddrService() {
        return customerAddrService;
    }
    @Autowired
    public void setCustomerAddrService(CustomerAddrService customerAddrService) {
        this.customerAddrService = customerAddrService;
    }

    public ChinaService getChinaService() {
        return chinaService;
    }
    @Autowired
    public void setChinaService(ChinaService chinaService) {
        this.chinaService = chinaService;
    }
}
